﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TeamService
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Dictionary<int, Page> _content;
        bool _started;
        ViewModal.TeamServieceViewModal _viewModal;

        public MainWindow()
        {
            InitializeComponent();

            _content = new Dictionary<int, Page>();
            _content.Add(0, new Pages.Index() { DataContext = this.DataContext });
            _content.Add(1, new Pages.Production() { DataContext = this.DataContext });
            _content.Add(2, new Pages.Teams() { DataContext = this.DataContext });
            _content.Add(3, new Pages.Work() { DataContext = this.DataContext });
            _content.Add(4, new Pages.ManagerIndex() { DataContext = this.DataContext });
            _content.Add(5, new Pages.About() { DataContext = this.DataContext });
            _started = false;
        }
        public MainWindow(ViewModal.TeamServieceViewModal vm)
        {
            this.DataContext = vm;
            vm.Navigate = Navigate;
            InitializeComponent();
            _content = new Dictionary<int, Page>();
            _content.Add(0, new Pages.Index() { DataContext = this.DataContext });
            _content.Add(1, new Pages.Production() { DataContext = this.DataContext });
            _content.Add(2, new Pages.Teams() { DataContext = this.DataContext });
            _content.Add(3, new Pages.Work() { DataContext = this.DataContext });
            _content.Add(4, new Pages.ManagerIndex() { DataContext = this.DataContext });
            _content.Add(5, new Pages.About() { DataContext = this.DataContext });
            _content.Add(6, new Pages.MemberManager() { DataContext = this.DataContext });
            _content.Add(7, new Pages.TeamManager() { DataContext = this.DataContext });
            _content.Add(8, new Pages.ProjectManager() { DataContext = this.DataContext });
            _started = false;
            _viewModal = vm;

        }

        private void SelectPage(object sender, SelectionChangedEventArgs e)
        {
            var list = sender as ListBox;
            var item = list.SelectedIndex;
            if (_started && item > 0)
            {
                Wrapper.Navigate(_content[item]);
                CurrentPage = item;
            }
            else
            {
                _started = true;
            }
        }

        private void Navigate(int num)
        {
            CurrentPage = num;
            Wrapper.Navigate(_content[num]);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _started = true;
        }

        public void Login(object sender, RoutedEventArgs e)
        {
            if (_viewModal.IsLogin)
            {
                _viewModal.IsLogin = false;
                _viewModal.AuthorRight.Right = 0;
                LoginButton.Content = "";
            }
            else
            {
                Login l = new Login();
                if (l.ShowDialog() == true)
                {
                    _viewModal.IsLogin = true;
                    _viewModal.RefreshAuthorRight(l.Member);
                    LoginButton.Content = "";
                }
            }
        }





        public static int CurrentPage { get; set; }

        #region ToolBar

        private void ProjectManage(object sender, RoutedEventArgs e)
        {
            Navigate(8);
            Navigator.SelectedIndex = -1;
        }

        private void TeamManage(object sender, RoutedEventArgs e)
        {
            Navigate(7);
            Navigator.SelectedIndex = -1;
        }

        private void MemberManage(object sender, RoutedEventArgs e)
        {
            Navigate(6);
            Navigator.SelectedIndex = -1;
        }

        private void AddMember(object sender, RoutedEventArgs e)
        {

            if (CurrentPage < 6)
            {
                Navigator.SelectedIndex = -1;
                Navigate(6);
            }

            if (CurrentPage == 6)
            {
                //在成员管理页面中，单击该按钮则是添加新成员
                var m = new Access.Member();
                _viewModal.Members.Add(m);
                var obj = _viewModal.Focus as Control;
                var l = _viewModal.Selector as ListBox;

                if (l != null)
                    l.SelectedItem = m;
                obj?.Focus();
            }
            else if (CurrentPage == 7)
            {
                //添加新队伍成员
                if (_viewModal.SelectItem != null)
                {
                    Select selector = new Select() { DataContext = DataContext };
                    if (selector.ShowDialog() == true)
                    {
                        if (_viewModal.SelectItem is Access.Team team)
                        {
                            var source = selector.Result.Union<Access.Member>(team.Member);
                            HashSet<Access.Member> set = new HashSet<Access.Member>(source);
                            var cha = set.Except(team.Member);
                            foreach(var i in cha)
                            {
                                team.Member.Add(i);
                            }
                        }
                    }

                }
            }
            else if (CurrentPage == 8)
            {
                //添加队伍
                if (_viewModal.SelectItem != null)
                {
                    if (_viewModal.SelectItem is Access.Project)
                        ((Access.Project)_viewModal.SelectItem).Teams.Add(new Access.Team());
                }
            }
        }

        private void RemoveItem(object sender, RoutedEventArgs e)
        {
            if (_viewModal.Selector != null)
            {
                var li = _viewModal.Selector as ListBox;
                var list = li.SelectedItems;
                if (list != null && list.Count > -1)
                {
                    lock (list)
                    {
                        foreach (var i in list)
                        {
                            switch (CurrentPage)
                            {
                                case 6:
                                    _viewModal.Members.Remove((Access.Member)i);
                                    break;
                                case 7:
                                    _viewModal.Team.Remove((Access.Team)i);
                                    break;
                                case 8:
                                    _viewModal.Projects.Remove((Access.Project)i);
                                    break;
                            }
                        }
                    }
                }
            }
        }

        private void SetLeader(object sender, RoutedEventArgs e)
        {
            if (_viewModal.Selector != null)
            {
                var li = _viewModal.Selector as ListBox;
                if (li != null)
                {
                    var item = li.SelectedItem;
                    if (item is Access.Project)
                    {
                        //缺人
                    }
                    else if (item is Access.Team)
                    {
                        //缺人
                    }
                    else
                    {
                    }
                }
            }
        }

        private void SetManager(object sender, RoutedEventArgs e)
        {

        }

        private void ShowMemberCommunication(object sender, RoutedEventArgs e)
        {
            if (_viewModal.SelectItem != null)
            {
                if (_viewModal.SelectItem is Access.Member mem)
                {
                    //show
                }
            }
        }

        private void AddItem(object sender, RoutedEventArgs e)
        {
            if (CurrentPage == 6)
            {
                _viewModal.Members.Add(new Access.Member());
                ((UIElement)_viewModal.Focus).Focus();

            }
            else if (CurrentPage == 7)
            {
                _viewModal.Team.Add(new Access.Team());
                ((UIElement)_viewModal.Focus).Focus();
            }
            else if (CurrentPage == 8)
            {
                _viewModal.Projects.Add(new Access.Project());
                ((UIElement)_viewModal.Focus).Focus();
            }

        }

        private void RemoveMember(object sender, RoutedEventArgs e)
        {
            switch (CurrentPage)
            {
                case 6:
                    lock (_viewModal.Members)
                    {
                        if (_viewModal.SelectItem != null)
                        {
                            if (_viewModal.SelectItem is IList<object> ilist)
                            {
                                for (int i = 0; i < ilist.Count; i++)
                                {
                                    if (ilist[i] is Access.Member mem)
                                    {
                                        _viewModal.Members.Remove(mem);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 7:
                    if (_viewModal.MemberList != null)
                    {
                        var li = _viewModal.MemberList as ListBox;
                        var items = li.SelectedItems;
                        if (items != null)
                        {
                            lock (items)
                            {
                                for (int i = 0; i < items.Count; i++)
                                {
                                    if (_viewModal.SelectItem is Access.Team team)
                                    {
                                        if (items[i] is Access.Member mem)
                                            team.Member.Remove(mem);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 8:
                    break;
            }

        }
        private void Refresh(object sender, RoutedEventArgs e)
        {

        }

        private void Update(object sender, RoutedEventArgs e)
        {

        }

        private void MultiSelect(object sender, RoutedEventArgs e)
        {
            if (CurrentPage > 5)
            {
                var li = _viewModal.Selector as ListBox;
                if (li != null)
                {
                    li.SelectionMode = li.SelectionMode == SelectionMode.Multiple ? SelectionMode.Single : SelectionMode.Multiple;
                }
            }
        }
        #endregion


    }
}
